//
//  LCEnrollmentViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/11.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCEnrollmentViewController.h"
#import "LCAcitviceDetailViewController.h"
#import "LCServiceCell.h"
#import "LCHeadView.h"
#import "ActivityModel.h"
#import "CountHeight.h"
static NSString *cellidentifier = @"LCServiceCell";
@interface LCEnrollmentViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _page;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableDictionary  *dataDic;
@property (strong,nonatomic)NSMutableArray       *dataArray;

@end

@implementation LCEnrollmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"活动报名"];
    self.rightImg.hidden = YES;
    self.rightBtn.hidden = YES;
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"LCServiceCell" bundle:nil] forCellReuseIdentifier:cellidentifier];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{//下拉刷新
        _page = 1;
        [self loadData];
    }];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{//上拉刷新
        [self loadData];
    }];
    [_tableView.mj_header beginRefreshing];
}
-(void)loadData
{
    if (_page == 1)
    {
        [_dataArray removeAllObjects];
    }
    [_dataDic setObject:@"10" forKey:@"pageSize"];
    [_dataDic setObject:[NSString stringWithFormat:@"%ld",_page] forKey:@"pageNum"];
   [MDataProvider activityListBodyDic:_dataDic sucess:^(NSArray *array)
   {
       [_dataArray addObjectsFromArray:array];
       _page++;
       [_tableView reloadData];
       [_tableView.mj_header endRefreshing];
       [_tableView.mj_footer endRefreshing];
       if (array.count<10)
       {
           self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
       }
       else
       {
           self.tableView.mj_footer.state = MJRefreshStateIdle;
       }
   } faile:^(NSString *str)
    {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:str];
   }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LCHeadView *headView = [[LCHeadView alloc]initWithTitle:@"全部活动"];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
    if (_dataArray.count>0)
    {
        ActivityModel *model = [_dataArray objectAtIndex:indexPath.row];
        cell.titleLbl.text = model.event_title;
        cell.contentLbl.text = model.event_summary;
        NSString *imgUrl = [NSString stringWithFormat:@"%@/%@%@",ENCLOUD_URL,model.file_path,model.file_name];
        [cell.iconImg sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        if(![model.event_start isEqualToString:@""])//实际开始时间不为空
        {
            cell.stateLbl.text = @"已结束";
            cell.stateLbl.backgroundColor = [UIColor colorWithHex:0x079907];
        }
        else//实际开始时间为空
        {
            NSDate *currentDate = [NSDate date];//获取当前时间
            int compare = [CountHeight compareDate:model.expect_start withDate:currentDate];
            if (compare == -1)//当前时间小于预计开始时间
            {
                if (model.is_signup)//判断能报名
                {
                    int  compare2 = [CountHeight compareDate:model.signup_start withDate:currentDate];
                    if (compare2 == -1)//当前时间小于报名开始时间
                    {
                        cell.stateLbl.text = @"未开始";
                        cell.stateLbl.backgroundColor = [UIColor colorWithHex:0xff8a00];
                    }
                    else //当前时间大于等于报名开始时间
                    {
                        int  compare3 = [CountHeight compareDate:model.signup_end withDate:currentDate];
                        if (compare3!=-1)//当前时间小于等于报名结束时间
                        {
                            cell.stateLbl.text = @"报名中";
                            cell.stateLbl.backgroundColor = [UIColor colorWithHex:0xff1d00];
                        }
                        else
                        {
                            cell.stateLbl.text = @"未开始";
                            cell.stateLbl.backgroundColor = [UIColor colorWithHex:0xff8a00];
                        }
                    }
                }
                else
                {
                    cell.stateLbl.text = @"未开始";
                    cell.stateLbl.backgroundColor = [UIColor colorWithHex:0xff8a00];
                }
            }
            else //当前时间大于等于预计开始时间
            {
                 int compare3 = [CountHeight compareDate:model.expect_end withDate:currentDate];
                if (compare3 != 1) //当前时间小于等于预计结束时间
                {
                    cell.stateLbl.text = @"报名中";
                    cell.stateLbl.backgroundColor = [UIColor colorWithHex:0xff1d00];
                }
                else
                {
                    cell.stateLbl.text = @"已结束";
                    cell.stateLbl.backgroundColor = [UIColor colorWithHex:0x079907];
                }
            }
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCServiceCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    LCAcitviceDetailViewController *vc = [[LCAcitviceDetailViewController alloc]init];
    ActivityModel *model = [_dataArray objectAtIndex:indexPath.row];
    vc.event_id = model.event_id;
    vc.state = cell.stateLbl.text;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
